<template>
  <div class="Dialog">
    <el-dialog :title="forms.title" :visible.sync="forms.flag">
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="form.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="form.goods_price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="form.goods_number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="form.goods_weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="介绍"
          prop="goods_introduce"
          v-show="forms.type == 'add'"
        >
          <el-input
            v-model="form.goods_introduce"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item v-show="forms.type == 'add'">
          <span class="demonstration">商品分类：</span>
          <el-cascader
            v-model="value"
            :options="options"
            :props="{ label: 'cat_name', value: 'cat_id' }"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="商品图片" v-show="forms.type == 'add'">
          <el-upload
            class="upload-demo"
            action="http://82.156.12.132:8080/api/private/v1/upload"
            :headers="headers"
            :on-success="handlePreview"
            :on-remove="handleRemove"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="forms.flag = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addList, editList } from "@/utils/Api(goods)/Goods";
export default {
  props: { forms: Object, form: Object, options: Array },
  data() {
    return {
      value: [],
      headers: {},
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        goods_introduce: [
          { required: true, message: "请输入商品介绍", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleRemove(file) {
      var a = file.response.data.tmp_path;
      var index = this.form.pics.findIndex((item) => item.pic == a);
      this.form.pics.splice(index, 1);
    },
    handlePreview(res) {
      console.log(res);
      this.form.pics.push({ pic: res.data.tmp_path });
    },
    async submitForm() {
      if (this.forms.type == "add") {
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            this.form.goods_cat = this.value.join(";");
            console.log(this.form);
            let data = await addList(this.form);
            this.forms.flag = false;
            if (data.meta.status == 201) {
              this.$message.success(data.meta.msg);
            } else {
              this.$message.error(data.meta.msg);
            }
            this.$emit("add");
          } else {
            return false;
          }
        });
      } else if (this.forms.type == "edit") {
        let data = await editList(this.form.id, this.form);
        this.forms.flag = false;
        if (data.meta.status == 200) {
          this.$message.success(data.meta.msg);
        } else {
          this.$message.error(data.meta.msg);
        }
        this.$emit("add");
      }
    },
  },
  mounted() {
    var token = sessionStorage.getItem("token") || "";
    this.headers.Authorization = token;
  },
};
</script>

<style></style>
